package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.asn1.axdr.AxdrEnum;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * FollowReport类
 * 实现跟随报告的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class FollowReport implements AxdrType {

    private byte[] dataCode = null;
    private FollowReportChoice choice = FollowReportChoice._ERR_NONE_SELECTED;
    private SubSeqOfResultNormal resultNormal = null;
    private SubSeqOfResultRecord resultRecord = null;

    public FollowReport() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setResultNormal(SubSeqOfResultNormal resultNormal) {
        this.resetChoices();
        this.choice = FollowReportChoice.RESULT_NORMAL;
        this.resultNormal = resultNormal;
    }

    public void setResultRecord(SubSeqOfResultRecord resultRecord) {
        this.resetChoices();
        this.choice = FollowReportChoice.RESULT_RECORD;
        this.resultRecord = resultRecord;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = FollowReportChoice.fromValue(choosen.getValue());

        if (this.choice == FollowReportChoice.RESULT_NORMAL) {
            resultNormal = new SubSeqOfResultNormal();
            codeLength += resultNormal.decode(input);
            return codeLength;
        } else if (this.choice == FollowReportChoice.RESULT_RECORD) {
            resultRecord = new SubSeqOfResultRecord();
            codeLength += resultRecord.decode(input);
            return codeLength;
        }

        throw new RuntimeException("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        if (this.dataCode != null) {
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return this.dataCode.length;
        }
        if (this.choice == FollowReportChoice._ERR_NONE_SELECTED) {
            throw new RuntimeException("Error encoding AxdrChoice: No item in choice was selected.");
        }

        int codeLength = 0;

        if (this.choice == FollowReportChoice.RESULT_RECORD) {
            if (resultRecord != null) {
                codeLength += resultRecord.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(2);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("resultRecord is null");
            }
        } else if (this.choice == FollowReportChoice.RESULT_NORMAL) {
            if (resultNormal != null) {
                codeLength += resultNormal.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("resultNormal is null");
            }
        }

        throw new RuntimeException("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    public FollowReportChoice getChoice() {
        return this.choice;
    }

    public void resetChoices() {
        this.choice = FollowReportChoice._ERR_NONE_SELECTED;
        this.resultNormal = null;
        this.resultRecord = null;
    }

    @Override
    public String toString() {
        if (this.choice == FollowReportChoice.RESULT_RECORD) {
            if (this.resultRecord != null) {
                return "choice: {resultRecord: " + this.resultRecord + "}";
            } else {
                return "choice is resultRecord but resultRecord is null";
            }
        } else if (this.choice == FollowReportChoice.RESULT_NORMAL) {
            if (this.resultNormal != null) {
                return "choice: {resultNormal: " + this.resultNormal + "}";
            } else {
                return "choice is resultNormal but resultNormal is null";
            }
        } else {
            return "unknown";
        }
    }
}